From 3723373e59e33505f9b768088374b03a286f6314 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 21 Jul 2009 23:20:00 +0000 Subject: [PATCH] Add experimental support for a total hit count in the MySQL search engine. Disabled by default as it's a bit flaky when you combine title and page content results... also might be slow on larger sites. :) Good for local UI testing to confirm that the search count totals work in the search UI/API though! /** * Set to true to have the default MySQL search engine count total * search matches to present in the Special:Search UI. * * This could however be slow on larger wikis, and is pretty flaky * with the current title vs content split. Recommend avoiding until * that's been worked out cleanly; but this may aid in testing the * search UI and API to confirm that the result count works. */ $wgSearchMySQLTotalHits = false; --- includes/DefaultSettings.php | 11 ++++++++++ includes/SearchMySQL.php | 40 +++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index af406d9a6d..290d698aa6 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1908,6 +1908,17 @@ $wgAdvancedSearchHighlighting = false; $wgSearchHighlightBoundaries = version_compare("5.1", PHP_VERSION, "<")? '[\p{Z}\p{P}\p{C}]' : '[ ,.;:!?~!@#$%\^&*\(\)+=\-\\|\[\]"\'<>\n\r\/{}]'; // PHP 5.0 workaround +/** + * Set to true to have the default MySQL search engine count total + * search matches to present in the Special:Search UI. + * + * This could however be slow on larger wikis, and is pretty flaky + * with the current title vs content split. Recommend avoiding until + * that's been worked out cleanly; but this may aid in testing the + * search UI and API to confirm that the result count works. + */ +$wgSearchMySQLTotalHits = false; + /** * Template for OpenSearch suggestions, defaults to API action=opensearch * diff --git a/includes/SearchMySQL.php b/includes/SearchMySQL.php index 6709033a86..3d04466ba6 100644 --- a/includes/SearchMySQL.php +++ b/includes/SearchMySQL.php @@ -148,8 +148,22 @@ class SearchMySQL extends SearchEngine { * @return MySQLSearchResultSet */ function searchText( $term ) { - $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) ); - return new MySQLSearchResultSet( $resultSet, $this->searchTerms ); + global $wgSearchMySQLTotalHits; + + $filteredTerm = $this->filter( $term ); + $resultSet = $this->db->query( $this->getQuery( $filteredTerm, true ) ); + + $total = null; + if( $wgSearchMySQLTotalHits ) { + $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, true ) ); + $row = $totalResult->fetchObject(); + if( $row ) { + $total = $row->c; + } + $totalResult->free(); + } + + return new MySQLSearchResultSet( $resultSet, $this->searchTerms, $total ); } /** @@ -221,8 +235,7 @@ class SearchMySQL extends SearchEngine { $this->queryRanking( $filteredTerm, $fulltext ) . ' ' . $this->queryLimit(); } - - + /** * Picks which field to index on, depending on what type of query. * @param $fulltext Boolean @@ -251,6 +264,17 @@ class SearchMySQL extends SearchEngine { 'WHERE page_id=si_page AND ' . $match; } + function getCountQuery( $filteredTerm, $fulltext ) { + $match = $this->parseQuery( $filteredTerm, $fulltext ); + $page = $this->db->tableName( 'page' ); + $searchindex = $this->db->tableName( 'searchindex' ); + return "SELECT COUNT(*) AS c " . + "FROM $page,$searchindex " . + 'WHERE page_id=si_page AND ' . $match . + $this->queryRedirect() . ' ' . + $this->queryNamespaces(); + } + /** * Create or update the search index record for the given page. * Title and text should be pre-processed. @@ -292,9 +316,10 @@ class SearchMySQL extends SearchEngine { * @ingroup Search */ class MySQLSearchResultSet extends SearchResultSet { - function MySQLSearchResultSet( $resultSet, $terms ) { + function MySQLSearchResultSet( $resultSet, $terms, $totalHits=null ) { $this->mResultSet = $resultSet; $this->mTerms = $terms; + $this->mTotalHits = $totalHits; } function termMatches() { @@ -317,4 +342,9 @@ class MySQLSearchResultSet extends SearchResultSet { function free() { $this->mResultSet->free(); } + + + function getTotalHits() { + return $this->mTotalHits; + } } \ No newline at end of file -- 2.20.1